package com.gmail.drheiniger.book.organizer;

import java.awt.Component;
import java.awt.Container;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;

import com.gmail.drheiniger.backend.Book;
import com.gmail.drheiniger.backend.Database;

public class OverviewGUI implements ActionListener {

	// making these variables static means that I can access them by typing
	// OverviewGUI.staticString
	// i don't know about the private static ones.

	// constants
	private static final String TITLE_LABEL_TEXT = "WELCOME TO THE BOOK ORGANIZER!";
	private static final String ADD_BOOK_BUTTON_TEXT = "Add Books";
	private static final String SEARCH_BOOKS_BUTTON_TEXT = "Search";
	private static final String BROWSE_BUTTON_TEXT = "Browse Books";
	private static final String STATISTICS_BUTTON_TEXT = "Statistics";
	private static final String CLOSE_BUTTON_TEXT = "Close";

	// initialize components
	private JFrame frame;
	private JLabel titleLabel = new JLabel(TITLE_LABEL_TEXT);
	private JButton addBookButton = new JButton(ADD_BOOK_BUTTON_TEXT);
	private JButton searchBooksButton = new JButton(SEARCH_BOOKS_BUTTON_TEXT);
	private JButton browseBookButton = new JButton(BROWSE_BUTTON_TEXT);
	private JButton statisticsButton = new JButton(STATISTICS_BUTTON_TEXT);
	private JButton closeButton = new JButton(CLOSE_BUTTON_TEXT);

	public OverviewGUI() {

		frame = new JFrame("Book Organizer");
		Container contentPane = frame.getContentPane();
		GridBagLayout layout = new GridBagLayout();
		GridBagConstraints c = new GridBagConstraints();
		contentPane.setLayout(layout);

		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		// c.gridwidth = GridBagConstraints.;

		titleLabel.setFont(new Font("Dialog", 1, 18));
		titleLabel.setOpaque(true);
		// titleLabel.setBackground(Color.BLACK);
		titleLabel.setHorizontalAlignment(SwingConstants.CENTER);
		titleLabel.setBorder(BorderFactory.createRaisedBevelBorder());

		c.fill = GridBagConstraints.HORIZONTAL;
		c.weightx = 1;
		c.weighty = 1;
		c.ipady = 120;
		c.gridx = 0;
		c.gridy = 0;

		contentPane.add(titleLabel, c);

		c.fill = GridBagConstraints.HORIZONTAL;
		c.ipady = 50;
		c.gridx = 0;
		c.gridy = 1;
		contentPane.add(addBookButton, c);

		c.fill = GridBagConstraints.HORIZONTAL;
		// c.ipady = 30;
		c.gridx = 0;
		c.gridy = 2;
		contentPane.add(searchBooksButton, c);

		c.fill = GridBagConstraints.HORIZONTAL;
		// c.ipady = 30;
		c.gridx = 0;
		c.gridy = 3;
		contentPane.add(browseBookButton, c);

		c.fill = GridBagConstraints.HORIZONTAL;
		// c.ipady = 30;
		c.gridx = 0;
		c.gridy = 4;
		contentPane.add(statisticsButton, c);

		c.fill = GridBagConstraints.HORIZONTAL;
		// c.ipady = 30;
		c.gridx = 0;
		c.gridy = 5;
		contentPane.add(closeButton, c);

		addBookButton.addActionListener(this);
		searchBooksButton.addActionListener(this);
		browseBookButton.addActionListener(this);
		statisticsButton.addActionListener(this);
		closeButton.addActionListener(this);
		
		this.frame.setSize(400, 500);
		// frame.setLocation(x / 2, y / 2);
		this.frame.setLocationRelativeTo(null);
		this.frame.setResizable(false);
		this.frame.setVisible(true);
	}

	public void actionPerformed(ActionEvent e) {
		Component source = null;
		String srcAction = "";
		source = (JButton) (e.getSource());
		srcAction = ((JButton) source).getText();
		switch (srcAction) {
		case ADD_BOOK_BUTTON_TEXT:
			// go to add book page
			new GUI();
			frame.dispose();
			break;
		case SEARCH_BOOKS_BUTTON_TEXT:
			// go to search books page
			new SearchBooksGUI();
			frame.dispose();
			break;
		case BROWSE_BUTTON_TEXT:
			// go to browse books page
			Book book = new Book();
			Database db = new Database();
			db.setPrepareStatement(book);
			new BrowseGUI(db);
			frame.dispose();
			break;
		case STATISTICS_BUTTON_TEXT:
			JOptionPane.showMessageDialog(frame, "Coming Soon");
			break;
		case CLOSE_BUTTON_TEXT:
			// close program
			frame.dispose();
			break;
		}
	}
}